<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] &raquo; ".__("Member List");
$page_title=__("Member List");

if(!$pv["task"]["Members"]){
 $errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to manage Members."));
            
 print format_admin_page($errmsg, $this_title);
 exit();
}

$add_edit_file=$vars["file"]["admin"]["add_edit_member"];

$item_per_page=50;

//checkbox search, prefix "_op_"
$search_options["status"]["option"] = $r_status=array("normal","suspended","terminated");
$search_options["status"]["option_display"] = $r_status_d=array("normal"=>__("Normal"),"suspended"=>__("Suspended"),"terminated"=>__("Terminated"));
$search_options["status"]["display"] = __("Status");
$search_options["is_stockist"]["option"] = $r_is_stockist=array("y","n");
$search_options["is_stockist"]["option_display"] = $r_is_stockist_d=array("y"=>__("Yes"),"n"=>__("No"));
$search_options["is_stockist"]["display"] = __("Is Stockist");
$search_options["gender"]["option"] = $r_gender=array("m","f");
$search_options["gender"]["option_display"] = $r_gender_d=array("m"=>__("Male"),"f"=>__("Female"));
$search_options["gender"]["display"] = __("Gender");
$search_options["total_unit"]["option"] = $r_total_unit=array("y");
$search_options["total_unit"]["option_display"] = $r_total_unit_d=array("y"=>__("Yes"));
$search_options["total_unit"]["display"] = __("Share more than zero");
$country_code=$vars['common']['country_code'];
$country_code_t=$vars['common']['country_code_text'];
foreach($country_code_t as $code=>$cname){
 $country_code_d[$code]=__($cname);
}
$country_code_d['']=__("All");

//#####SEARCH#####
if($_GET["s"]){
 $searching=1;
 $search_query=member_search($_GET);
}

//textbox search, prefix "_sp_"
$r_textsearch=array("id"=>__("Member ID"),"username"=>__("Username"),"name"=>__("Name"),"code"=>__("Code"),"ic"=>__("I/C No."),"email"=>__("Email"),"address"=>__("Address"),"city"=>__("City"),"zip"=>__("Zip / Postal Code"),"state"=>__("State / Province"));

//custom search fields
//country
$custom_country['title']=__("Country");
$custom_country['value']=build_select($country_code, $country_code_d, $get_s["_sp_country"], "_sp_country", $inputbox_style);

$searchfields_ordering=array("id","username","name","code","ic","gender","email","address","city","zip","state",$custom_country,"is_stockist","status","total_unit");
$search_ui=build_admin_search_ui($searching, $this_file, $search_options, $r_textsearch, $searchfields_ordering, $inputbox_style);
//#####END SEARCH#####

//#####LIST USER#####
if($searching){
 $count_sql=$search_query["count_sql"];
 $record_sql=$search_query["record_sql"];/*  if($_SESSION["aid"]==2){		$count_sql.=" AND id>=44 and id not in (46,47,48,49,50,51,52,53,54,55,207,208,209,210,211,212,213,214)";		$record_sql=str_replace('group by gg_users.id order by id limit <%limit_start%>, <%limit%>',' AND gg_users.id>=44 and gg_users.id not in (46,47,48,49,50,51,52,53,54,55,207,208,209,210,211,212,213,214) group by gg_users.id order by id limit <%limit_start%>, <%limit%>', $record_sql);	}*/
}else{
 $count_sql="select count(*) from $db->users";
 $record_sql="select gg_users.*, sum(gg_share.total_unit) as total_unit from $db->users left join gg_share on gg_share.uid=gg_users.id and gg_share.sell_price='0.00' and gg_share.replica=0 group by gg_users.id order by id limit <%limit_start%>, <%limit%>";/*	if($_SESSION["aid"]==2){		$count_sql="select count(*) from $db->users WHERE id>=44 and id not in (46,47,48,49,50,51,52,53,54,55,207,208,209,210,211,212,213,214)";		$record_sql="select gg_users.*, sum(gg_share.total_unit) as total_unit from $db->users left join gg_share on gg_share.uid=gg_users.id and gg_share.sell_price='0.00' and gg_share.replica=0 WHERE gg_users.id>=44 and gg_users.id not in (46,47,48,49,50,51,52,53,54,55,207,208,209,210,211,212,213,214) group by gg_users.id order by id limit <%limit_start%>, <%limit%>";	}*/
}

$cur_page=$_GET["nav"];
$total_record=@mysql_result(mysql_query($count_sql), 0);
$total_page=ceil($total_record/$item_per_page);
$page=($cur_page? ($cur_page>$total_page? $total_page : ($cur_page<1? 1 : $cur_page)) : 1);
$limit_start=($page-1)*$item_per_page;
$nav_link=format_page_admin($page, $total_page, $this_file);
$this_page_total=@mysql_num_rows($r=mysql_query(str_replace("<%limit_start%>", $limit_start, str_replace("<%limit%>", $item_per_page, $record_sql))));
$k=0;
for($i=0;$i<$this_page_total;$i++){
 $user=mysql_fetch_assoc($r);
 if($user["ref"] && !$r_ref[$user["ref"]]){
  $r_ref[$user["ref"]]=get_user_detail_by_id($user['ref']);
 }
 $edit_link="<a href=\"$add_edit_file?type=edit&id=$user[id]\"><img src='".M_URL."/images/icons/edit.png'></a>";
 $user_row.="
 <tr class='row$k'>
  <td>$edit_link</td>
  <td>$user[id]</td>
  <td>$user[code]</td>
  <td>$user[name]</td>
  <td>$user[username]</td>".($pv['task']['View Member Password']? "
  <td>$user[password]</td>" : "")."
  <td>".($user['rank_a']? $vars['rank_index_a_d'][$user['rank_a']] : __("None"))."</td>
  <td>".number_format($user['ewallet'], 2)."</td>  <td>".number_format($user['eswallet'], 2)."</td>  <td>".number_format($user['cwallet'], 2)."</td>  <td>".number_format($user['mwallet'], 2)."</td>  <td>".number_format($user['owallet'], 2)."</td>  <td>".number_format($user['swallet'], 2)."</td>
  <td>".($user['ref']? __("ID").__(":").$r_ref[$user['ref']]['id']." ".__("Name").__(":")." ".$r_ref[$user['ref']]['name'] : __("None"))."</td>
  <td>".$r_status_d[$user["status"]]."</td>
  <td>".($user["acc_type"]=="c"? "N/A" : $r_gender_d[$user["gender"]])."</td>
  <td>".($user["acc_type"]=="c"? $user["bis_reg"] : $user["ic"])."</td>
  <td>".$r_is_stockist_d[$user['is_stockist']]."</td>
  <td>$user[email]</td>
  <td>$user[city]</td>
  <td>$user[zip]</td>
  <td>$user[state]</td>
  <td>".$country_code_d[$user['country']]."</td>
  <td>$user[mobileno]</td>
  <td>$user[homeno]</td>
  <td>$user[officeno]</td>
  <td>$user[faxno]</td>
  <td>$user[bank_name]</td>
  <td>$user[bank_acc_no]</td>
  <td>".date($vars["date_format"], strtotime($user['cdate']))."</td>
 </tr>";
 $k=1-$k;
}

//listing text
$listing_text=replace_tag($searching? __("Search returned <%x%> <%member%>.") : __("Listing <%x%> <%member%>."), array("<%x%>"=>strval($total_record), "<%member%>"=>$total_record>1? __("Members") : __("Member"))).($searching? " ".replace_tag(__("<%link%>Clear Search and Refresh<%/link%>."), array("<%link%>"=>"<a href='$this_file'>", "<%/link%>"=>"</a>")) : "");
$total_ewallet=@mysql_result(mysql_query("select sum(ewallet) from gg_users where id>10000"), 0);$total_mwallet=@mysql_result(mysql_query("select sum(mwallet) from gg_users where id>10000"), 0);$total_swallet=@mysql_result(mysql_query("select sum(swallet) from gg_users where id>10000"), 0);
//listing
$user_list=
"
$search_ui<br />\n
$listing_text $nav_link
<table class='aml_table' style='width:2000px;'>
 <tr class='aml_header'>
  <td width='18'><img src='".M_URL."/images/icons/edit.png'></td>
  <td>".__("ID")."</td>
  <td>".__("Code")."</td>
  <td>".__("Name")."</td>
  <td>".__("Username")."</td>".($pv['task']['View Member Password']? "
  <td>".__("Password")."</td>" : "")."
  <td>".__("Rank")."</td>
  <td>".__($vars['ewallet_title'])."</td>  <td>".__($vars['eswallet_title'])."</td>  <td>".__($vars['cwallet_title'])."</td>  <td>".__($vars['mwallet_title'])."</td>  <td>".__($vars['owallet_title'])."</td>  <td>".__($vars['swallet_title'])."</td>
  <td>".__("Referrer")."</td>
  <td>".__("Status")."</td>
  <td>".__("Gender")."</td>
  <td>".__("I/C No. / Bis Reg. No.")."</td>
  <td>".__("Stockist")."</td>
  <td>".__("Email")."</td>
  <td>".__("City")."</td>
  <td>".__("Zip")."</td>
  <td>".__("State")."</td>
  <td>".__("Country")."</td>
  <td>".__("Mobile Phone")."</td>
  <td>".__("Home No.")."</td>
  <td>".__("Office No.")."</td>
  <td>".__("Fax No.")."</td>
  <td>".__("Bank Name")."</td>
  <td>".__("Bank Acc No.")."</td>
  <td width='135'>".__("Joined Date")."</td>
 </tr>
 $user_row
</table>
$nav_link";
//#####END USER LIST#####

$content="<h2>$page_title</h2>\n$user_list";
           
print format_admin_page($content, $this_title);

function member_search($in_get){
 global $vars, $search_options;
 $db=$vars["db"];

 $get=$in_get;
 $search_type="and";
 $r_like_search=array("name","code","username","email","address","city","zip","state");
 //format search string
 foreach($get as $field=>$value){
  $value=trim($value);
  //search checkbox option
  if(preg_match('/^_so_/', $field)){
   preg_match('/(?<=_so_).+(?=__)/', $field, $matchf);
   $f=$matchf[0];
   preg_match('/(?<=__).+$/', $field, $matchv);
   $v=$matchv[0];
   $r_searchquery[$f].=($r_searchquery[$f]? "," : "")."'$v'";
  }
  //db field name search
  if(substr($field, 0, strlen("_sp_"))=="_sp_"){
   if(strlen($value)){
    $db_fieldname=substr($field, strlen("_sp_"));
    $db_fieldname_q.=($db_fieldname_q? " and " : "")."(".(in_array($db_fieldname, $r_like_search)? format_search_field_like($db_fieldname, $value) : format_search_field($db_fieldname, $value)).")";
   }
  }
 }
 if(isset($r_searchquery)){
  foreach($r_searchquery as $field => $query){
   if($field=="total_unit"){
   	if($query=="'y'")
   	$option_q.=($option_q? " and " : "")."$field > 0";
   }else{
   	$option_q.=($option_q? " and " : "")."$field in ($query)";
   }
  }
 }else{
  $option_q="1=1";
 }
 $db_fieldname_q=$db_fieldname_q? $db_fieldname_q : "1=1";

 $count_sql="select count(*) from $db->users where $option_q and $db_fieldname_q";
 $record_sql="select * from $db->users where $option_q and $db_fieldname_q order by id limit <%limit_start%>, <%limit%>";
 
 return array("count_sql"=>$count_sql, "record_sql"=>$record_sql);
}
?>